package com.edu.base.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;


/**
 * 微博第三方登录
 * @author techbuddy
 *
 */
public class WeiboUtil {
	  
    private Logger logger = LoggerFactory.getLogger(WeiboUtil.class);
 
     public static String CLIENT_ID = "";
     public final static String CLIENT_SERCRET = "";
     public final static String GET_TOKEN_URL = "https://api.weibo.com/oauth2/access_token";
     public final static String REDIRECT_URI = "http://localhost:9090/logback/weibo.jsp";
     public final static String GET_USER_INFO = "https://api.weibo.com/2/users/show.json";
     public final static String GET_TOKEN_INFO_URL = "https://api.weibo.com/oauth2/get_token_info";
     public final static String STATE = "register";
    
    /**
     * 获取AccessToken
     */
    public JSONObject getAccessToken(String code) {
        StringBuilder sb = new StringBuilder();
        sb.append("grant_type=authorization_code");
        sb.append("&client_id=" + CLIENT_ID);
        sb.append("&client_secret=" + CLIENT_SERCRET);
        sb.append("&redirect_uri=" + REDIRECT_URI);
        sb.append("&code=" + code);
        JSONObject result = HttpClientUtils.httpPost(GET_TOKEN_URL,sb.toString());
       
        return result;
    }
     
    /**
     * 获取用户信息
     * @param access_token
     * @param uid 查询的用户ID
     * @return
     */
    public JSONObject getUserInfo(String access_token,String uid){
        StringBuilder sb = new StringBuilder();
        sb.append("?access_token=" + access_token);
        sb.append("&uid=" + uid);
        JSONObject result = HttpClientUtils.httpGet(GET_USER_INFO+sb.toString()); 
        return result;
    }
}
